package cn.com.nes.site.service.workOrder.impl;

import cn.com.nes.common.em.TimeType;
import cn.com.nes.common.exception.BusinessException;
import cn.com.nes.common.exception.MissRequestParameterException;
import cn.com.nes.common.util.DESUtil;
import cn.com.nes.common.util.DateContUtil;
import cn.com.nes.common.util.Globals;
import cn.com.nes.common.util.TimeUtils;
import cn.com.nes.mybatis.agent.ope.entity.Sblx;
import cn.com.nes.mybatis.agent.order.entity.TSbazddPushCtce;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.em.CtceCglyEnum;
import cn.com.nes.site.entity.em.project.ProjectTypeEnum;
import cn.com.nes.site.entity.vo.workOrder.AgentPushCtceDataVO;
import cn.com.nes.site.entity.vo.workOrder.GetWorkOrderInstallDetailVO;
import cn.com.nes.site.service.crm.customer.dto.CustomerDTO;
import cn.com.nes.site.service.crm.customer.dto.CustomerHouseDTO;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityInfoDto;
import cn.com.nes.site.service.monitor.dto.EquipmentInfoDTO;
import cn.com.nes.site.service.product.SblxService;
import cn.com.nes.site.service.workOrder.AgentPushCtceService;
import cn.com.nes.site.service.workOrder.TSbazddPushCtceService;
import cn.com.nes.site.service.workOrder.WorkOrderService;
import cn.com.nes.site.service.workOrder.dto.WorkOrderInstallDetailDTO;
import cn.com.nes.site.service.workOrder.dto.WorkOrderInstallInfoDTO;
import cn.com.nes.site.service.workOrder.dto.WorkOrderInstallPicDTO;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AgentPushCtceServiceImpl implements AgentPushCtceService {

    @Resource
    private WorkOrderService workOrderService;

    @Resource
    private SblxService sblxService;

    @Resource
    private TSbazddPushCtceService tSbazddPushCtceService;

    @Override
    public Integer agentPushCtceData(AgentPushCtceDataVO vo) throws Exception {
        //参数校验
        generateAgentPushCtceData(vo);
        //查询工单详情
        GetWorkOrderInstallDetailVO getWorkOrderInstallDetailVO = new GetWorkOrderInstallDetailVO();
        getWorkOrderInstallDetailVO.setWorkOrderId(vo.getWorkOrderId());
        getWorkOrderInstallDetailVO.setOrderCreateUserId(vo.getOrderCreateUserId());
        getWorkOrderInstallDetailVO.setOrderBelongCompanyId(vo.getOrderBelongCompanyId());

        getWorkOrderInstallDetailVO.setUserInfo(vo.getUserInfo());
        getWorkOrderInstallDetailVO.setOperateUserCallCenterCompanyId(vo.getOperateUserCallCenterCompanyId());
        getWorkOrderInstallDetailVO.setOperateUserCompanyType(vo.getOperateUserCompanyType());
        PageData getWorkOrderInstallDetail = workOrderService.getWorkOrderInstallDetail(getWorkOrderInstallDetailVO);
        WorkOrderInstallDetailDTO workOrderInstallDetailDTO = (WorkOrderInstallDetailDTO) getWorkOrderInstallDetail.getData().get("workOrderInstallDetail");
        if (workOrderInstallDetailDTO == null) {
            throw new BusinessException("工单详情不能为空");
        }
        //只推送 政府项目数据
        if (workOrderInstallDetailDTO.getProjectDetail() != null
                && workOrderInstallDetailDTO.getProjectDetail().getProjectType().equals(ProjectTypeEnum.GOVERNMENT.getKey())) {
        //转为清洁取暖数据结构
            List<TSbazddPushCtce> tSbazddPushCtces = gettSbazddPushCtce(vo, workOrderInstallDetailDTO);
            tSbazddPushCtceService.insertTSbazddPushCtceList(tSbazddPushCtces);
        }
        return 1;
    }

    private void generateAgentPushCtceData(AgentPushCtceDataVO vo) {
//        if (StrUtil.isEmpty(vo.getOrderCreateUserId())){
//            throw new MissRequestParameterException("订单归属人ID不能为空");
//        }
//        if (StrUtil.isEmpty(vo.getOrderBelongCompanyId())){
//            throw new MissRequestParameterException("订单归属公司不能为空");
//        }
        if(vo.getUserInfo() == null){
            throw new MissRequestParameterException("登录用户信息不能为空");
        }
        if (StrUtil.isEmpty(vo.getOperateUserCallCenterCompanyId())){
            throw new MissRequestParameterException("操作用户长效管护公司ID不能为空");
        }
        if (StrUtil.isEmpty(vo.getOperateUserCompanyType())){
            throw new MissRequestParameterException("操作用户公司类型不能为空");
        }
    }

    @NotNull
    private List<TSbazddPushCtce> gettSbazddPushCtce(AgentPushCtceDataVO vo,WorkOrderInstallDetailDTO workOrderInstallDetailDTO) throws Exception {
        List<TSbazddPushCtce> tSbazddPushCtceList = new ArrayList();
        WorkOrderInstallInfoDTO workOrderInfo = workOrderInstallDetailDTO.getWorkOrderInfo();
        Map<String, WorkOrderInstallPicDTO> picList = workOrderInstallDetailDTO.getPicList()
                .stream().collect(Collectors.toMap(
                        WorkOrderInstallPicDTO -> WorkOrderInstallPicDTO.getField(),
                        WorkOrderInstallPicDTO -> WorkOrderInstallPicDTO
                ));
        CustomerDTO customerDetail = workOrderInstallDetailDTO.getCustomerDetail();
        List<CustomerHouseDTO> houseList = workOrderInstallDetailDTO.getHouseList();
        CustomerHouseDTO customerHouseDTO = CollUtil.isEmpty(houseList) ? new CustomerHouseDTO() : houseList.get(0) ;
        OpportunityInfoDto opportunityInfo = workOrderInstallDetailDTO.getOpportunityInfo();

        Date date = new Date();
        String qualityGuaranteeYears = workOrderInfo.getQualityGuaranteeYears();
        Date installDate = workOrderInfo.getInstallDate();
        //计算质保日期
        Date zbrq = DateContUtil.getTopYearDate(StrUtil.isEmpty(qualityGuaranteeYears) ? 0 : Integer.valueOf(qualityGuaranteeYears),
                TimeUtils.getStringByDate(installDate == null ? new Date() : installDate, TimeType.yyyy_MM_dd_HH_mm_ss));

        int xh = 2;
        for (EquipmentInfoDTO equipmentInfoDTO : workOrderInstallDetailDTO.getEquipmentInfoList()) {
            TSbazddPushCtce tSbazddPushCtce = new TSbazddPushCtce();
            tSbazddPushCtce.setXxdz(workOrderInfo.getAddress());
            tSbazddPushCtce.setSsxzqh(customerHouseDTO.getAreaCode());
            tSbazddPushCtce.setHzxm(customerDetail.getCustomerName());
            tSbazddPushCtce.setSfzh(DESUtil.encrypt(customerDetail.getIdCardNumber(), Globals.DESKEY));
            tSbazddPushCtce.setKhbh(customerDetail.getCustomerCode());
            tSbazddPushCtce.setMph(workOrderInfo.getHouseNumber());
            tSbazddPushCtce.setLxdd(customerDetail.getPhone());
            tSbazddPushCtce.setJzmj(customerHouseDTO.getFloorSpace());
            tSbazddPushCtce.setJzjg(customerHouseDTO.getBuildingStructureName());
            tSbazddPushCtce.setJzcg(customerHouseDTO.getBuildingHeight());
            tSbazddPushCtce.setWqhd(new BigDecimal(StrUtil.isEmpty(customerHouseDTO.getOuterWallThicknessName()) ? "0" : customerHouseDTO.getOuterWallThicknessName()));
            tSbazddPushCtce.setCnmd(customerHouseDTO.getHeatingEndName());
            tSbazddPushCtce.setSnsjwd(null);
            tSbazddPushCtce.setWqbw(customerHouseDTO.getOuterWallKeepWarm() == null ? "无" : (customerHouseDTO.getOuterWallKeepWarm() ? "有" : "无"));
//            tSbazddPushCtce.setSjjy(null);
            tSbazddPushCtce.setJzazwz(customerHouseDTO.getInstallPositionName());
            tSbazddPushCtce.setRs(customerHouseDTO.getResidentCount());
            tSbazddPushCtce.setAzry(workOrderInfo.getHandleUserName());
            tSbazddPushCtce.setAzrydh(workOrderInfo.getContactPhone());
            tSbazddPushCtce.setAzrq(installDate);
//        tSbazddPushCtce.setBz();
            tSbazddPushCtce.setByn1(null);
            tSbazddPushCtce.setByn2(null);
            tSbazddPushCtce.setByn3(workOrderInfo.getProcureFund());
            tSbazddPushCtce.setByc1(null);
            tSbazddPushCtce.setByc2(null);
            tSbazddPushCtce.setByc3(customerHouseDTO.getAreaCode());
            tSbazddPushCtce.setByd1(new Date());
            tSbazddPushCtce.setByd2(null);
            tSbazddPushCtce.setByd3(null);
            tSbazddPushCtce.setAzt(null);
            tSbazddPushCtce.setSjt(null);
            tSbazddPushCtce.setPmt(null);
            tSbazddPushCtce.setYcip(null);
            tSbazddPushCtce.setYcdkh(null);
            tSbazddPushCtce.setWxry(null);
            tSbazddPushCtce.setWxrydh(null);
            tSbazddPushCtce.setWxsblx(null);
            tSbazddPushCtce.setXyId(null);
            tSbazddPushCtce.setYnztxz(customerDetail.getEnergyUseProperty());
/*          tSbazddPushCtce.setSgdwdm();
            tSbazddPushCtce.setSpurl(null);
            tSbazddPushCtce.setZjazqk();
            tSbazddPushCtce.setGxazqk(null);
            tSbazddPushCtce.setCnmdzt(null);
            tSbazddPushCtce.setCnmdcz(null);
            tSbazddPushCtce.setQlcd(null);
            tSbazddPushCtce.setPztd(null);
            tSbazddPushCtce.setDltjzb(null);
            tSbazddPushCtce.setSgzbsh(null);
            tSbazddPushCtce.setTsyh(null);
            tSbazddPushCtce.setSjsfsc(null);
            tSbazddPushCtce.setJlqk(null);
            tSbazddPushCtce.setSgjdsh(null);
            tSbazddPushCtce.setYycnmd(null);
            tSbazddPushCtce.setMqqnfs(null);
            tSbazddPushCtce.setNygyzb(null);
            tSbazddPushCtce.setZjdmj(null);
            tSbazddPushCtce.setGqqnfy(null);
            tSbazddPushCtce.setScrq(null);
            tSbazddPushCtce.setRlbbh(null);*/
            tSbazddPushCtce.setZtbs("12");//施工中  到清洁取暖还需要审核
            tSbazddPushCtce.setSbazsj(null);
            tSbazddPushCtce.setDlqnfj(null);
            tSbazddPushCtce.setCnmj(customerHouseDTO.getCommonHeatingArea());
            if (picList.containsKey("design")) {
                tSbazddPushCtce.setSjtpath(picList.get("design").getPath());
                tSbazddPushCtce.setSjtfilename(picList.get("design").getField());
            }
            if (picList.containsKey("planView")) {
                tSbazddPushCtce.setPmtpath(picList.get("planView").getPath());
                tSbazddPushCtce.setPmtfilename(picList.get("planView").getField());
            }
            if (picList.containsKey("idCardAndDevice")) {
                tSbazddPushCtce.setAztpath1(picList.get("idCardAndDevice").getPath());
                tSbazddPushCtce.setAztfilename1(picList.get("idCardAndDevice").getField());
            }
            if (picList.containsKey("nameplateAndBarcode")) {
                tSbazddPushCtce.setAztpath2(picList.get("nameplateAndBarcode").getPath());
                tSbazddPushCtce.setAztfilename2(picList.get("nameplateAndBarcode").getField());
            }
            if (picList.containsKey("deviceFront")) {
                tSbazddPushCtce.setAztpath3(picList.get("deviceFront").getPath());
                tSbazddPushCtce.setAztfilename3(picList.get("deviceFront").getField());
            }
            if (picList.containsKey("deviceSide")) {
                tSbazddPushCtce.setAztpath4(picList.get("deviceSide").getPath());
                tSbazddPushCtce.setAztfilename4(picList.get("deviceSide").getField());
            }
            if (picList.containsKey("entranceDoorIncludesHouseNumber")) {
                tSbazddPushCtce.setAztpath5(picList.get("entranceDoorIncludesHouseNumber").getPath());
                tSbazddPushCtce.setAztfilename5(picList.get("entranceDoorIncludesHouseNumber").getField());
            }
            if (picList.containsKey("heatingBuilding")) {
                tSbazddPushCtce.setAztpath6(picList.get("heatingBuilding").getPath());
                tSbazddPushCtce.setAztfilename6(picList.get("heatingBuilding").getField());
            }
            tSbazddPushCtce.setDbh(customerDetail.getEnergyMeterNumber());
            tSbazddPushCtce.setLrr("agentPushCtce");
         /*   tSbazddPushCtce.setShysqk(null);
            tSbazddPushCtce.setShyssj(null);
            tSbazddPushCtce.setRlbxh(null);
            tSbazddPushCtce.setDlbbh(null);
            tSbazddPushCtce.setDlbxh(null);
            tSbazddPushCtce.setLljbh(null);
            tSbazddPushCtce.setLljxh(null);
            tSbazddPushCtce.setCh(null);
            tSbazddPushCtce.setHcsx(null);
            tSbazddPushCtce.setJzmpfilename(null);
            tSbazddPushCtce.setJzmppath(null);
            tSbazddPushCtce.setSbmpfilename(null);
            tSbazddPushCtce.setSbmppath(null);
            tSbazddPushCtce.setJzgnbz(null);
            tSbazddPushCtce.setJzgnsm(null);
            tSbazddPushCtce.setMjsfxf(null);
            tSbazddPushCtce.setCssfzb(null);
            tSbazddPushCtce.setSbsfzb(null);
            tSbazddPushCtce.setSbsfxf(null);
            tSbazddPushCtce.setSfsfxf(null);
            tSbazddPushCtce.setSfyhdt(null);
            tSbazddPushCtce.setSfaz(null);
            tSbazddPushCtce.setZlsfqq(null);
            tSbazddPushCtce.setShrydh(null);
            tSbazddPushCtce.setShyj(null);
            tSbazddPushCtce.setXqlp(null);
            tSbazddPushCtce.setLh(null);
            tSbazddPushCtce.setDyh(null);
            tSbazddPushCtce.setLc(null);
            tSbazddPushCtce.setFjh(null);
            tSbazddPushCtce.setXssx(null);
            tSbazddPushCtce.setLb(null);*/
            tSbazddPushCtce.setGnfs(customerHouseDTO.getHeatingModeName());
            tSbazddPushCtce.setScddh(null);
            tSbazddPushCtce.setByc4(null);
            tSbazddPushCtce.setByc5(null);
            tSbazddPushCtce.setByc6(customerDetail.getPlanYear());
/*            tSbazddPushCtce.setByc7(null);
            tSbazddPushCtce.setByc8(null);
            tSbazddPushCtce.setByc9(null);
            tSbazddPushCtce.setByc10(null);
            tSbazddPushCtce.setByc11(null);
            tSbazddPushCtce.setByc12(null);
            tSbazddPushCtce.setByc13(null);
            tSbazddPushCtce.setByc14(null);
            tSbazddPushCtce.setByc15(null);
            tSbazddPushCtce.setByc16(null);
            tSbazddPushCtce.setByc17(null);
            tSbazddPushCtce.setByc18(null);
            tSbazddPushCtce.setByc19(null);
            tSbazddPushCtce.setByc20(null);
            tSbazddPushCtce.setSbscrq();
            tSbazddPushCtce.setSbccrq();
            tSbazddPushCtce.setSbfhrq();
            tSbazddPushCtce.setSbdhrq();
            tSbazddPushCtce.setSbzsyxrq(null);*/
            tSbazddPushCtce.setXtlx("CTCE");
            tSbazddPushCtce.setV1(null);
            tSbazddPushCtce.setLastModified(date);
            tSbazddPushCtce.setSfcz(StrUtil.isEmpty(customerHouseDTO.getEndState()) ? null : (customerHouseDTO.getEndState().equals("0") ? "01" : "02"));
            tSbazddPushCtce.setSfnc(customerHouseDTO.getIsVillage() == null ? null: (!customerHouseDTO.getIsVillage() ? "01" : "02"));
/*            tSbazddPushCtce.setGnqy(null);
            tSbazddPushCtce.setMjsfxfQ(null);
            tSbazddPushCtce.setCssfzbQ(null);
            tSbazddPushCtce.setSbsfzbQ(null);
            tSbazddPushCtce.setSbsfxfQ(null);
            tSbazddPushCtce.setSfsfxfQ(null);
            tSbazddPushCtce.setSfyhdtQ(null);
            tSbazddPushCtce.setSfazQ(null);
            tSbazddPushCtce.setZlsfqqQ(null);
            tSbazddPushCtce.setShysqkQ(null);
            tSbazddPushCtce.setShyssjQ(null);
            tSbazddPushCtce.setShysrQ(null);
            tSbazddPushCtce.setCzyhQ(null);*/
            tSbazddPushCtce.setZbrq(zbrq);
            tSbazddPushCtce.setZbnf(qualityGuaranteeYears);
            tSbazddPushCtce.setCgly(CtceCglyEnum.getVal(opportunityInfo.getRenovationType()).getName());
//            tSbazddPushCtce.setYhzt();
            tSbazddPushCtce.setFwjs(customerHouseDTO.getRoomCount() == null ? "" : customerHouseDTO.getRoomCount().toString());
            tSbazddPushCtce.setFwyt(customerHouseDTO.getHousePerposeName());
            tSbazddPushCtce.setSjdw(customerHouseDTO.getDesignUnit());
            tSbazddPushCtce.setTzbh(customerHouseDTO.getDrawingNo());
//            tSbazddPushCtce.setSfcysb();
            tSbazddPushCtce.setCycnmj(customerHouseDTO.getCommonHeatingArea() == null ? "" : customerHouseDTO.getCommonHeatingArea().toString());
            tSbazddPushCtce.setChlx(StrUtil.isEmpty(customerHouseDTO.getWindowType()) ? null : Integer.valueOf(customerHouseDTO.getWindowType()));
            tSbazddPushCtce.setSjrfh(customerHouseDTO.getDesignHeatingLoad());
            tSbazddPushCtce.setFwcx(StrUtil.isEmpty(customerHouseDTO.getHouseDirection()) ? null : Integer.valueOf(customerHouseDTO.getHouseDirection()));
            tSbazddPushCtce.setIsPush(0);
            tSbazddPushCtce.setGmtCreateId(vo.getOperateUserId());
            tSbazddPushCtce.setGmtCreate(date);

            String sblxId = equipmentInfoDTO.getSblxId();
            Sblx tSblxOperation = sblxService.selectBySblxId(sblxId);
            tSbazddPushCtce.setDdId(equipmentInfoDTO.getDdId());
            tSbazddPushCtce.setSbId(equipmentInfoDTO.getSbId());
            tSbazddPushCtce.setSbLxid(sblxId);
            tSbazddPushCtce.setWxId(equipmentInfoDTO.getWxId());
            tSbazddPushCtce.setJd(equipmentInfoDTO.getJd());
            tSbazddPushCtce.setWd(equipmentInfoDTO.getWd());
            tSbazddPushCtce.setYnfsdl(tSblxOperation.getYnfsdl());
            tSbazddPushCtce.setYnfsxl(tSblxOperation.getYnfsxl());
            tSbazddPushCtce.setSyny(tSblxOperation.getSyny());
            tSbazddPushCtce.setSynymc(tSblxOperation.getSynymc());
            tSbazddPushCtce.setRxl(tSblxOperation.getRxl());
            tSbazddPushCtce.setSbpp(tSblxOperation.getSbpp());
            tSbazddPushCtce.setSbxh(tSblxOperation.getSbxh());
            tSbazddPushCtce.setJldw(equipmentInfoDTO.getUnitName());
            if (equipmentInfoDTO.getSfzj() == 1) {
                tSbazddPushCtce.setXh(1);
            } else {
                tSbazddPushCtce.setXh(xh++);
            }
            tSbazddPushCtceList.add(tSbazddPushCtce);
            //185
        }
        return tSbazddPushCtceList;
    }

}
